<template>
  <div class="warehouseUpload">
    <el-form :model="form" ref="form" label-width="150px" class="demo-ruleForm" style="text-align: center;">
      <el-row>
        <normalForm :formList="form.list" @input="changelist" @change="changelist" />
      </el-row>
    </el-form>
    <div class="btn">
      <el-button type="primary" @click="uploadData" class="upload" plain>提交信息</el-button>
      <el-button type="warning" @click="resetForm" plain class="clean">清空输入</el-button>
      <el-button type="info" @click="returnTo" plain>返回首页</el-button>
    </div>
  </div>
</template>

<script>
  import normalForm from "../../../components/GoodsComponent/GoodsForm.vue"
  import { warehouseAdd, warehouseEdit } from "../../../http/apis/goodsApi/warehouse.js";
  import { mapState } from 'vuex'

  export default {
    components: {
      normalForm
    },
    data() {
      return {
        form: {
          list: [{
            id: 0,
            name: '仓库名称',
            data: '',
            span: 24,
          }, {
            id: 1,
            name: '负责人',
            data: '',
            span: 24,
          }, {
            id: 2,
            name: '电话',
            data: '',
            span: 24,
          }, {
            id: 3,
            name: '备注',
            data: '',
          }],
        },
        sh_id: 0,
      };
    },
    computed: {
      ...mapState('goodsStore', ['ADType']),
    },
    methods: {
      // 修改表单数据
      changelist(list) {
        this.form.list = list
      },
      // 上传信息
      uploadData() {
        let data = {
          "responsible": this.form.list[1].data,
          "sh_comment": this.form.list[3].data,
          "sh_id": this.sh_id,
          "sh_status": 0,
          "sh_phone": this.form.list[2].data,
          "storage_name": this.form.list[0].data
        }
        console.log(12343, data)
        if (this.ADType == 'add') {
          warehouseAdd(data)
            .then((res) => {
              if (res.data.code == "200") {
                this.$message({
                  type: "success",
                  message: "信息添加成功",
                  showClose: true,
                })
                this.$router.push('/goods/warehouse/warehouseMsg')
              }
            })
            .catch((err) => {
              console.log("请求失败", err);
              this.$message({
                type: "error",
                message: "请求失败，请稍后再试",
                showClose: true,
              });
            });
        } else if (this.ADType == 'edit') {
          console.log(data)
          warehouseEdit(data)
            .then((res) => {
              console.log("成功请求：", res.data.code);
              if (res.data.code == "200") {
                this.$message({
                  type: "success",
                  message: "信息修改成功",
                  showClose: true,
                })
                this.$router.push('/goods/warehouse/warehouseMsg')
              }
            })
            .catch((err) => {
              console.log("请求失败", err);
              this.$message({
                type: "error",
                message: "请求失败，请稍后再试",
                showClose: true,
              });
            });
        }
      },
      // 清空条件
      resetForm() {
        this.gt_pno = ''
        this.gt_name = ''
        for (let key in this.form.list) {
          this.form.list[key].data = ''
        }
      },
      // 返回首页
      returnTo() {
        this.$confirm('即将返回首页，所做的操作无法保存, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          this.$router.push('/goods/warehouse/warehouseMsg')
        }).catch(() => {
          this.$message({
            type: "info",
            message: "已取消跳转",
            showClose: true,
          });
        });
      }
    },
    created() {
      if (this.ADType == "edit") {
        let msg = this.$route.query.editmsg
        this.sh_id = msg.sh_id
        this.form.list[0].data = msg.storage_name
        this.form.list[1].data = msg.responsible
        this.form.list[2].data = msg.sh_phone
        this.form.list[3].data = msg.sh_comment
      }
    }
  };
</script>

<style>
  .warehouseUpload {
    margin-top: 50px;
  }

  .el-form {
    text-align: center;
  }

  .upload,
  .clean {
    margin: 50px;
  }
</style>